#ifndef ETHERCATSLAVE_H
#define ETHERCATSLAVE_H

#include <map>
#include <memory>
#include <vector>
#include <string>
#include "ethercat.h"
//#include "EthercatMaster.h"

namespace ECatDrv
{

#define CIA_APP_VERSION         0x3000

// 主站状态
enum masterState
{
    M_IDLE      =   0,
    M_SAFE_OP   =   1,
    M_OP        =   2,
    M_ERROR     =   3,
    M_PRE_OP    =   4,
    M_BOOT      =   5,
    M_INIT      =   6,
    M_OTHER     =   7
};

enum slaveType
{
    S_ServoDrive        =   0,
    S_SafetyModule      =   1,
    S_Other             =   2,
};

// 从站信息
//struct slaveInfo
//{
//    unsigned int eep_man;       // 制造商编号
//    unsigned int eep_id;        // 设备ID
//    std::string slave_name;     // 从站名字
//    int slaveAddr;              // 从站地址
//    slaveType slaveType;        // 从站类型
//};


class EthercatSlave
{
public:
    unsigned int s_eep_man;
    unsigned int s_eep_id;
    std::string slave_name;
    int slave_addr;
    slaveType slave_type;
    // EthercatMaster* master_ptr;

    EthercatSlave() = default;
    EthercatSlave(unsigned int eep_man, unsigned int eep_id, const std::string& slave_name, int slave_addr,
                  slaveType s_type);

    virtual ~EthercatSlave() = default;

    // 子类重写，用于配置PO2SO
    virtual int configurePDO() = 0;

    // 静态包装器：给PO2SOconfig调用使用
    static int pdoConfigWrapper(uint16 slave_addr);

    // 将从站注册进静态映射表中
    static void registerSlave(uint16 slaveAddr, std::shared_ptr<EthercatSlave> slave);


private:
    // 从站地址 ➜ 实例指针，用于包装器查找
    static std::map<uint16, std::shared_ptr<EthercatSlave>> slaveRegistry;
};

}

#endif // ETHERCATSLAVE_H
